﻿using System;
using System.Collections.Generic;
using System.IO;
using TransferModels;

namespace DBController
{
    [Serializable]
    public class AccountDB
    {
        public AccountDB(string path)
        {
            this.path = path;
            if (File.Exists(path))
            {
                try
                {
                    byte[] buffer = File.ReadAllBytes(path);
                    AccountDB dB = RRQMXml.XmlTool.DeserializeWithBinary<AccountDB>(buffer, 0, buffer.Length);
                    this.UserInfos = dB.UserInfos;
                    return;
                }
                catch (Exception)
                {
                }
            }

            this.UserInfos = new Dictionary<string, UserInfo>();
        }

        private string path;

        public Dictionary<string, UserInfo> UserInfos { get; private set; }

        /// <summary>
        /// 注册账号
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <param name="mes"></param>
        /// <returns></returns>
        public bool Register(string account, string password, out string mes)
        {
            if (this.UserInfos.ContainsKey(account))
            {
                mes = "此账号已被注册";
                return false;
            }
            try
            {
                UserInfo userInfo = new UserInfo();
                userInfo.Account = account;
                userInfo.Password = password;
                userInfo.Friends = new List<string>();

                this.UserInfos.Add(account, userInfo);
                this.Save();
                mes = null;
                return true;
            }
            catch (Exception e)
            {
                mes = e.Message;
                return false;
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Login(string account, string password)
        {
            if (this.UserInfos.ContainsKey(account))
            {
                UserInfo user = this.UserInfos[account];
                if (user.Account == account && user.Password == password)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public UserInfo GetUserinfo(string account)
        {
            if (this.UserInfos.ContainsKey(account))
            {
                UserInfo user = this.UserInfos[account];
                return user;
            }
            return null;
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="userinfo"></param>
        /// <returns></returns>
        public bool UpdataUserinfo(UserInfo userinfo, out string mes)
        {
            if (this.UserInfos.ContainsKey(userinfo.Account))
            {
                this.UserInfos[userinfo.Account] = userinfo;
                try
                {
                    this.Save();
                    mes = null;
                    return true;
                }
                catch (Exception e)
                {
                    mes = e.Message;
                    return false;
                }
            }
            else
            {
                mes = "账号错误";
                return false;
            }
        }

        /// <summary>
        /// 添加好友列表
        /// </summary>
        public bool AddFriendList(string selfAccount, string friendAccount, out string mes)
        {
            if (this.UserInfos.ContainsKey(selfAccount) && this.UserInfos.ContainsKey(friendAccount))
            {
                UserInfo user = this.UserInfos[selfAccount];
                user.Friends.Add(friendAccount);
                try
                {
                    this.Save();
                    mes = null;
                    return true;
                }
                catch (Exception e)
                {
                    mes = e.Message;
                    return false;
                }
            }
            else
            {
                mes = "账号错误";
                return false;
            }
        }

        public FriendList GetFriendList(string account)
        {
            FriendList users = new FriendList();
            if (this.UserInfos.ContainsKey(account))
            {
                UserInfo user = this.UserInfos[account];
                foreach (string friendAccount in user.Friends)
                {
                    if (this.UserInfos.ContainsKey(friendAccount))
                    {
                        users.Add(this.UserInfos[friendAccount]);
                    }
                }
            }
            return users;
        }

        public void Save()
        {
            File.WriteAllBytes(this.path, RRQMXml.XmlTool.SerializeToBinary(this));
        }
    }
}